package roleimpl

import (
	"context"
	"go-caipu/pkg/services/admin/model"
	"go-caipu/pkg/services/admin/role"
	"go-caipu/pkg/util"
	"gorm.io/gorm"
)

type Service struct {
	store store
}

func ProvideService(db *gorm.DB) (*Service, error) {
	store := ProvideStore(db)
	s := &Service{
		store: &store,
	}
	return s, nil
}
func (s Service) GetRolePage(ctx context.Context, cmd role.GetRolePageCommand) ([]role.Role, int64, error) {
	items, count, err := s.store.GetRolePage(ctx, cmd)
	if err != nil {
		return nil, 0, err
	}
	var results []role.Role
	for _, item := range items {
		results = append(results, makeRole(item))
	}
	return results, count, nil
}

func makeRole(sysRole model.SysRole) role.Role {
	result := role.Role{
		RoleId:    sysRole.RoleId,
		RoleName:  sysRole.RoleName,
		RoleKey:   sysRole.RoleKey,
		DataScope: util.StringToInt(sysRole.DataScope),
		Flag:      sysRole.Flag,
		Admin:     sysRole.Admin,
		Status:    sysRole.Status,
		RoleSort:  sysRole.Sort,
		Remark:    sysRole.Remark,
		CreatedAt: sysRole.CreatedAt.Format("2006-01-02 15:04:05"),

		MenuIds: sysRole.MenuIds,
		DeptIds: sysRole.DeptIds,
	}

	return result
}

func (s Service) GetRole(ctx context.Context, cmd role.GetRoleCommand) (role.Role, error) {
	item, err := s.store.GetRole(ctx, cmd)
	if err != nil {
		return role.Role{}, err
	}
	return makeRole(item), nil
}

func (s Service) Insert(cmd role.SysRoleInsertReq) error {
	//此处直接使用数据库对象，后面可以改为dto对象
	var model model.SysRole
	cmd.Generate(&model)
	err := s.store.Insert(&model)
	if err != nil {
		return err
	}
	//更新数据权限
	var dataCmd role.UpdateDataScopeCommand
	dataCmd.DeptIds = cmd.DeptIds
	dataCmd.RoleId = model.RoleId
	dataCmd.DataScope = model.DataScope
	s.store.UpdateDataScope(dataCmd)
	return s.store.UpsertPolicy(util.Int64ToString(model.RoleId), cmd.MenuIds)
}

func (s Service) Update(cmd role.SysRoleUpdateReq) error {
	var model model.SysRole
	cmd.Generate(&model)
	err := s.store.Update(model)
	if err != nil {
		return err
	}
	//更新数据权限
	var dataCmd role.UpdateDataScopeCommand
	dataCmd.DeptIds = cmd.DeptIds
	dataCmd.RoleId = model.RoleId
	dataCmd.DataScope = model.DataScope
	s.store.UpdateDataScope(dataCmd)
	return s.store.UpsertPolicy(util.Int64ToString(cmd.RoleId), cmd.MenuIds)
}

func (s Service) Remove(ctx context.Context, cmd role.DelRoleCommand) error {
	return s.store.Remove(cmd)
	//if err != nil {
	//	return err
	//}
	//return s.store.UpsertPolicy(util.IntToString(role.RoleId), nil)
}
func (s Service) GetPermissionByRole(cmd []string) ([]string, error) {
	return s.store.GetPermissionByRole(cmd)
}
func (s Service) GetRoleDataScope(ctx context.Context, roleId int64) ([]string, error) {
	return s.store.GetDataScope(roleId)
}
func (s Service) UpdateDataScope(ctx context.Context, cmd role.UpdateDataScopeCommand) (err error) {
	return s.store.UpdateDataScope(cmd)
}
func (s Service) UpdateRolePermission(ctx context.Context, cmd role.UpdateRolePermissionCommand) (err error) {
	return s.store.UpdateRolePermission(ctx, cmd)
}
func (s Service) GetUserIdsByRoleId(roleId string) ([]string, error) {
	return s.store.GetUserIdsByRoleId(roleId)
}
func (s Service) RemoveRoleUser(ctx context.Context, roleId string, userIds []string) error {
	return s.store.RemoveRoleUser(ctx, roleId, userIds)
}
func (s Service) AddRoleUser(ctx context.Context, roleId string, userIds []string) error {
	return s.store.AddRoleUser(ctx, roleId, userIds)
}

// BatchCheckEnforce 批量检查权限
// 入参：
// request [][]interface{} 二维数组，每个子数组包含三个元素，分别是subject、object、action
// 返回值：
// []bool 每个子数组的权限检查结果
// error 错误信息
func (s Service) BatchCheckEnforce(request [][]interface{}) ([]bool, error) {
	return s.store.BatchCheckEnforce(request)
}
